package fr.demos.cinepantin.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import fr.demos.cinepantin.enumerate.Anciennete;
import fr.demos.cinepantin.enumerate.Categorie;
import fr.demos.cinepantin.enumerate.Tva;
import fr.demos.cinepantin.exception.*;
import fr.demos.cinepantin.Article;

import fr.demos.cinepantin.dao.DaoArticle;

import fr.demos.cinepantin.dao.interfac.ArticleDao;

@DaoArticle
public class ArticleDaoImpl implements ArticleDao {

	// Grace a l'injection l'objet entityManager est instancie
	@PersistenceContext(unitName = "cinemarc")
	private EntityManager em; // gestion des entity qui sont dans un contexte
	@Resource
	private UserTransaction ut;

	public ArticleDaoImpl() {

	}

	@Override
	public void addArticle(Article a) throws DaoExceptions {
		try {
			ut.begin();
		} catch (Exception e) {
			e.printStackTrace();
		}

		em.persist(a);

		try {
			ut.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoExceptions(e.getMessage(), e);
		}
	}

	@Override
	public Article getArticleParId(int id) throws DaoExceptions {

		Article article = null;
		article = em.find(Article.class, id);

		return article;

	}

	@Override
	public List<Article> getArticleByTitre(String titre) throws DaoExceptions {

		String query = "SELECT a FROM Article a WHERE";
		query += " titre LIKE '%" + titre + "%'";
		TypedQuery<Article> q = em.createQuery(query, Article.class);
		List<Article> resArticle = q.getResultList();
		return resArticle;

	}

	@Override
	public List<Article> getAllArticle() throws DaoExceptions {
		
		// Article article = null;
		String query = "select a from Article a ";
		TypedQuery<Article> q = em.createQuery(query, Article.class);
		List<Article> resArticle = q.getResultList();

		return resArticle;

	}

	@Override
	public List<Article> getArticleByLivre(Categorie categorie) throws DaoExceptions {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Article> getArticleByDvd(Categorie categorie) throws DaoExceptions {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void modifierArticle(Article a) throws DaoExceptions {
		try {
			ut.begin();
		} catch (Exception e) {
			e.printStackTrace();
		}

		em.merge(a);

		try {
			ut.commit();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DaoExceptions(e.getMessage(), e);
		}
		
	}

	@Override
	public Categorie getCategorieById(int id) throws DaoExceptions {
		Categorie categorie = null;
		String query = "select a.categorie from Article a where a.id=" +id;
		TypedQuery<Categorie> q = em.createQuery(query, Categorie.class);
		categorie = q.getSingleResult();

		return categorie;
	}

}
